package com.ossean.main;

import com.ossean.dao.OpenSourceProjectsDao;
import com.ossean.dao.RelativeMemoToOpenSourceProjectDao;
import com.ossean.dao.SimilarityDao;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.*;

/**
 * Created by zyr on 17/9/3.
 */

@Component
public class RelationCount {
    @Resource
    private RelativeMemoToOpenSourceProjectDao  relativeMemoToOpenSourceProjectDao ;
    @Resource
    private OpenSourceProjectsDao openSourceProjectsDao;
    @Resource
    private SimilarityDao similarityDao;
    
    private String openSourceProjectTable = "open_source_projects";
//    private String similarTable;
    
    public void getRelatedOsp(int startId,int endId){
    	List<Integer> prjList = openSourceProjectsDao.getOpenSourceProjectList4Relation(openSourceProjectTable);
    	int osp1,osp2;
    	float sim;
    	int count = 0;
    	int ospCount = 0;
    	long start_time = System.currentTimeMillis();
    	for(int i = startId;i <= endId;i++){
    		for(int j =i+1;j< prjList.size();j++){
    			osp1 = prjList.get(i);
    			osp2 = prjList.get(j);
				sim = compareRelativeMemos(osp1,osp2);
				count ++;
				if(count % 10000 == 0){
					long current_time = System.currentTimeMillis();
					System.out.println("have handled: "+count+" entrys");
					System.out.println("cost: "+(current_time - start_time)/1000/60+" mins");
				}
    			similarityDao.insertRecommends(getRecommendTable(osp1), osp1,osp2,sim);
    			similarityDao.insertRecommends(getRecommendTable(osp2), osp2,osp1,sim);
    		}
			ospCount++;
			System.out.println("current handled prj: "+prjList.get(i));
			if(ospCount % 500==0)
				System.out.println("we have handled: "+ospCount+" projects");

    	}
		System.out.println("we have handled: "+ospCount+" projects");
    }
    
    public float compareRelativeMemos(int osp1,int osp2){
    	String targetTable1 = getTargetTable(osp1);
    	List<Integer> osp1_memos_list = relativeMemoToOpenSourceProjectDao.getRelativeMemosByOspId(targetTable1, osp1);
    	String targetTable2 = getTargetTable(osp2);
    	List<Integer> osp2_memos_list = relativeMemoToOpenSourceProjectDao.getRelativeMemosByOspId(targetTable2, osp2);
    	int intersectionSize = CollectionUtils.intersection(osp1_memos_list, osp2_memos_list).size();
    	float minSize = osp1_memos_list.size() < osp2_memos_list.size()?osp1_memos_list.size():osp2_memos_list.size();
    	if(minSize !=0)
    		return (intersectionSize/minSize);
    	else
    		return 0;
    }

    public  String getTargetTable(int ospId){
		String targetTableName = "";
		if(ospId >= 770000){
			targetTableName = "relative_memo_to_open_source_projects_70";
		}
		else{
			int a = 1 + ospId/11000;
			targetTableName = "relative_memo_to_open_source_projects_" + a;
		}
		return targetTableName;
	}

	public  String getRecommendTable(int ospId){
		int a = ospId % 100;
		return "relation_recommends_" + a;
	}
    
    public static void main(String[] args){
        ApplicationContext aContext = new ClassPathXmlApplicationContext(
                "classpath:/applicationContext*.xml");
        RelationCount  rc = aContext.getBean(RelationCount.class);
        rc.getRelatedOsp(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
    }

}